import {
	router
} from './router.js';
import route_white_list from "./route_white_list";

const tabbarList = ['/pages/home/home', '/pages/consult/consult', '/pages/session_list/sessionList',
	'/pages/profile/profile'
]


const install = (Vue, vm) => {
	let timer;
	//全局路由前置守卫
	router.beforeEach(async (to, from, next) => {
		next();
	});

	// 全局路由后置守卫
	router.afterEach((to, from) => {
		const token = vm.getTokenSync();
		if (route_white_list.indexOf(to.path) == -1 && (!vm.userInfo || !token)) {
			uni.showModal({
				title: '提示',
				content: '本次操作需要您进行登录验证',
				success: (res) => {
					if (res.confirm) {
						vm.$u.route('/pages_login/login_entry/loginEntry');
					} else if (res.cancel) {
						console.log(from)
						if (tabbarList.indexOf(from.path) != -1) {
							vm.$u.route({
								url: from.path,
								type: 'switchTab'
							});
						} else {
							// vm.$u.route(from.path);
							uni.navigateBack();
						}

					}
				}
			});
		} else if (route_white_list.indexOf(to.path) == -1 && vm.userInfo && !vm.userInfo.customer_type &&
			to
			.path != '/pages_login/select_identity/selectIdentity' && to
			.path != '/pages/authentication/authentication') {
			clearTimeout(timer);
			timer = setTimeout(() => {
				uni.navigateTo({
					url: '/pages_login/select_identity/selectIdentity',
					success: () => {
						vm.$showToast('请完善个人资料')
					}
				});
			}, 1000)
		} else if (route_white_list.indexOf(to.path) == -1 && vm.userInfo && (!vm.userInfo.nickname || !vm
				.userInfo.avatars || !vm.userInfo.sex) && to
			.path != '/pages/authentication/authentication' &&
			to
			.path != '/pages_login/select_identity/selectIdentity') {
			clearTimeout(timer);
			timer = setTimeout(() => {
				uni.navigateTo({
					url: '/pages/authentication/authentication',
					success: () => {
						vm.$showToast('请完善个人资料')
					}
				});
			}, 1000)
		}
	})

}

export default {
	install
};
